<?php

namespace App\Http\Controllers\Api;

use App\Ad;
use App\Http\Requests\StoreAd;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Session\Store;
use Illuminate\Support\Facades\Storage;

class AdController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $orderBy = $request->order_by ? $request->order_by : 'order';
        $orderType = $request->order_type ? $request->order_type : 'asc';
        $pageSize = $request->page_size ? $request->page_size : $this->pageSize;
        $lists = Ad::orderBy($orderBy, $orderType)->with(['position']);
        if ($request->get('position')) {
            $lists = $lists->where('position_id', $request->position);
        }
        return $lists->paginate($pageSize);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreAd $request)
    {
        Ad::create($request->all());
        return response(['message' => '创建成功']);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Ad  $ad
     * @return \Illuminate\Http\Response
     */
    public function show(Ad $ad)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Ad  $ad
     * @return \Illuminate\Http\Response
     */
    public function edit(Ad $ad)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Ad  $ad
     * @return \Illuminate\Http\Response
     */
    public function update(StoreAd $request, Ad $ad)
    {
        $ad->update($request->only(['title', 'url', 'position_id', 'media_type', 'order', 'body']));
        return response(['message' => '修改成功']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Ad  $ad
     * @return \Illuminate\Http\Response
     */
    public function destroy(Ad $ad)
    {
        if ($ad->body && Storage::disk('public')->exists($ad->body))
            Storage::disk('public')->delete($ad->content);
        $ad->delete();
        return response(['message' => '删除成功']);
    }
    // 首页广告
    public function home()
    {
        //$focus = Ad::orderBy('order', 'asc')->where('position_id', 1)->limit(5)->get();
        $activities = Ad::orderBy('order', 'asc')->where('position_id', 3)->limit(4)->get();
        $big = Ad::orderBy('order', 'asc')->where('position_id', 2)->first();
        $f1 = Ad::orderBy('order', 'asc')->where('position_id', 4)->first();
        $f2 = Ad::orderBy('order', 'asc')->where('position_id', 5)->first();
        $f3 = Ad::orderBy('order', 'asc')->where('position_id', 6)->first();
        return response([
            //'focus' => $focus,
            'activities' => $activities,
            'big' => $big,
            'f1' => $f1,
            'f2' => $f2,
            'f3' => $f3,
        ]);
    }
}
